<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="cateVisible = true">添加分类</el-button>
      </div>
        <el-table
          border
          stripe
          :data="cateList"
          style="width: 100%">
          <el-table-column
          type='index'
            label="序号"
            width="180">
          </el-table-column>
          <el-table-column
            prop="cate_name"
            label="分类名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="cate_alias"
            label="分类别名">
          </el-table-column>
           <el-table-column
            label="操作">
            <template v-slot='{row}'>
            <el-button type='primary' size='mini' @click="hArtSubmit(row)">修改</el-button>
            <el-button type='danger' size='mini' @click="remArtSubmit(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-card>

    <el-dialog
      title="添加文章分类"
      :visible.sync="cateVisible"
      width="35%">
      <el-form :model="cateForm" :rules="cateRules" ref="cateForm" label-width="100px" class="demo-cateForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="cateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="cateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cateVisible = false">取 消</el-button>
        <el-button type="primary" @click="hSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改文章分类 -->
     <el-dialog
      title="修改文章分类"
      :visible.sync="artVisible"
      width="35%">
      <el-form :model="artForm" :rules="artRules" ref="artForm" label-width="100px" class="demo-artForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="artForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="artForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="artVisible = false">取 消</el-button>
        <el-button type="primary" @click="artVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      cateVisible: false,
      cateForm: {
        cate_name: '',
        cate_alias: ''
      },
      cateRules: {
        cate_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      },
      artVisible: false,
      artForm: {
        cate_name: '',
        cate_alias: ''
      },
      artRules: {
        cate_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async loadCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      // console.log(res)
      this.cateList = res.data
    },
    hSubmit() {
      this.$refs.cateForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.cateForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.cateVisible = false
        this.$refs.cateForm.resetFields()
        this.loadCateList()
      })
    },
    hArtSubmit(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('文章分类不可更改')
      this.artVisible = true
      this.artForm = row
    },
    async  remArtSubmit(id) {
      if (id === 1 || id === 2) return this.$message.error('文章分类不可删除')
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.loadCateList()
    }
  },
  mounted() {
    this.loadCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
